#setLanguageLevel 4;

//Script: Contact to Visma Net - version 03-01-2025 13:00 by SuperOffice InfoBridge

//--Configuration start
//This is the only part of the script where configuration per tenant is needed.
//In variables and other code we use Contact for company (UI and documentation) 
//and we use Person for a contact (UI and documentation). 

//The user friendly name of the ERP system.
String erpPlatform = "Visma Net";

//You can find the INSTANT job URL in the Process step of the DataBridge Integration. (Note: make sure to take the INSTANT job URL).
String instantJobUrl = "";

//The user defined field that indicates if a company card is linked to a company card in ERP. Fill in the Prog ID.
//A bool will be checked for 0/1; a text or number will be checked whether the field is empty.
String fieldContactIsLinkedToERP = "COMPANYLINKEDTOERP";

//The user defined field that indicates if a contact card is linked to a contact card in ERP. Fill in the Prog ID.
//A bool will be checked for 0/1; a text or number will be checked whether the field is empty.
String fieldPersonIsLinkedToERP = "CONTACTLINKEDTOERP";

//--Configuration end


EventData ed = getEventData();
String contactId = ed.getInputValue("ActiveContactId");
String contactName = ed.getInputValue("PersonEntity.Contact.FullName");
String personId = ed.getInputValue("ActivePersonId");
String personName = ed.getInputValue("PersonEntity.FullName");
String contactIsLinkedToERP_Value = "";
Bool contactIsLinkedToERP = false;
String personIsLinkedToERP_Value = ed.getInputValue("PersonEntity.UserDefinedFields." + fieldPersonIsLinkedToERP);
Bool personIsLinkedToERP = false;

if (!contactId.isEmpty() && contactId.toInteger() > 0)
{
    NSContactAgent agent;
    NSContactEntity contact = agent.GetContactEntity(contactId.toInteger());
    Map fields = contact.GetUserDefinedFields();
    contactIsLinkedToERP_Value = fields.get(fieldContactIsLinkedToERP);
}

if (!contactIsLinkedToERP_Value.isEmpty() && contactIsLinkedToERP_Value == "True")
{
    contactIsLinkedToERP = true;
}

//Updates from SuperOffice to ERP are not allowed
if (!contactIsLinkedToERP)
{
    ed.setMessage("\r\nThe company '" + contactName +"' is not available in " + erpPlatform + ". Create the company in " + erpPlatform + " first.");
}
else if (!contactId.isEmpty() && contactId.toInteger() > 0 && !contactName.isEmpty() && !personId.isEmpty() && personId.toInteger() > 0 && !personName.isEmpty()) 
{
    //Check if the contact is already sent to ERP.
    if (!personIsLinkedToERP_Value.isEmpty() && personIsLinkedToERP_Value == "True")
    {
        personIsLinkedToERP = true;
    }
    String personOperation;
    if (personIsLinkedToERP) {personOperation = "updated";} else {personOperation = "created";}

    //Call the DataBridge instant job and wait for its response.
    try {
        HTTP h;
        XMLNode root = parseJSON(String(h.get(instantJobUrl + "&PersonId=" + personId)));
        String linesProcessed = root.getValueFromPath("LinesProcessed");

        if (!linesProcessed.isEmpty() && linesProcessed.toInteger() >= 1)
        {
            ed.setMessage("\r\nThe contact '" + personName +"' is " + personOperation + ".");

            //Set the 'Is linked to ERP' field to 'True'
            NSPersonAgent agent;
            NSPersonEntity person = agent.GetPersonEntity(personId.toInteger());
            Map fields;
            fields.insert(fieldPersonIsLinkedToERP, "[I:1]");
            person.SetUserDefinedFields(fields);
            agent.SavePersonEntity(person);
        }
        else
        {
            ed.setMessage("\r\nThe contact '" + personName +"' could not be " + personOperation + ".\r\n\r\nCheck the values in " + erpPlatform + ".");
        }

    }
    catch {
        ed.setMessage("Exception: \r\n" + error);
    }

}
else 
{
  ed.setMessage("\r\nThe contact details could not be found.\r\n\r\nThe contact was not created or updated in " + erpPlatform + ".");
}

ed.setBlockExecution(true);

